﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SoldierExam.Model;
using Yipusoft.SoldierExam.BLL;
using Yipusoft.Common.Model;
using Yipusoft.Common.BLL;

public partial class Aptitude_UpdateExam : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            BindYears();
            if (!string.IsNullOrEmpty(Request["ID"]))
            {
                bool isPermission = false;
                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300"))
                {
                    isPermission = true;
                }
                else
                {

                    if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300304"))
                    {
                        isPermission = true;
                    }

                }
                if (!isPermission)
                {
                    Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                    return;
                }
                ActionTitle.Text = "编辑考核题目";
            }
            else
            {
                bool isPermission = false;
                if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300300"))
                {
                    isPermission = true;
                }
                else
                {

                    if (Yipusoft.Account.BLL.User.HasPermission(User.Identity.Name, "300303"))
                    {
                        isPermission = true;
                    }

                }
                if (!isPermission)
                {
                    Response.Write("<script>alert('没有权限！');history.back(0);</script>");
                    return;
                }
                ActionTitle.Text = "添加考核题目";
            }
            DateBind();
        }
    }

    private void DateBind()
    {
        if (Request.QueryString["ID"] != null)
        {
            AptitudeExamInfo aptExam = AptitudeExam.GetAptitudeExamInfo(int.Parse(Request["ID"]));
            txtSubject.Text = aptExam.AptitudeExamSubject;
            ddlCount.SelectedValue = aptExam.ExamCount.ToString();
            ddlYear.SelectedValue = aptExam.ExamYear.ToString();
        }
        else
        {

        }
    }

    private void BindYears()
    {
        int year = DateTime.Now.Year;
        for (int i = -1; i <= 1; i++)
        {
            ListItem li = new ListItem(Convert.ToString(year + i));
            ddlYear.Items.Add(li);
        }
        ddlYear.SelectedValue = DateTime.Now.Year.ToString();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["ID"] != null)
        {
            Update();
        }
        else
        {
            Insert();
        }
    }

    private void Update()
    {
        AptitudeExamInfo exam = AptitudeExam.GetAptitudeExamInfo(int.Parse(Request["ID"]));
        exam.AptitudeExamSubject = txtSubject.Text;
        exam.ExamCount = int.Parse(ddlCount.SelectedValue);
        exam.ExamYear = int.Parse(ddlYear.SelectedValue);
        AptitudeExam.UpdateAptitudeExam(exam);
        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "更新了一条士兵资质考核题目";
        log.ItemID = Request["ID"];
        log.ItemName = "Combat";
        log.ItemFullUrl = "Aptitude/UpdateExam.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);
        Prompt.Text = "提示：更新成功！";
    }

    void Insert()
    {
        AptitudeExamInfo exam = new AptitudeExamInfo();
        exam.AptitudeExamSubject = txtSubject.Text;
        exam.ExamCount = int.Parse(ddlCount.SelectedValue);
        exam.ExamYear = int.Parse(ddlYear.SelectedValue);
        int examId = AptitudeExam.InsertAptitudeExam(exam);
        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "添加了一条编码";
        log.ItemID = examId.ToString();
        log.ItemName = "CodeItems";
        log.ItemFullUrl = "UpdateExam.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);
        Prompt.Text = "提示：添加成功！";
    }
}
